//登录页面高度自适应浏览器窗口高度
//$(".indexlogin").css("height",document.documentElement.clientHeight+"px");
//$(window).resize(function(){
//	$(".indexlogin").css("height",document.documentElement.clientHeight+"px");
//});

/**
 *  首页 合并
 **/
var validator_open_close_google = validator();
validator_open_close_google.validate({
	rules:{
		payPwd:{
			required:true,
			digit:6,
			param:{
				name:'payPwd'
			}
		},
		loginMsgCode: {
			required: function(){
				return checkSmsCode;
			},
			messageCode:true,
			param:{
				name:'msgCode'
			}
		},
		googleCodeWd: {
			required: function(){
				return checkGoogleCode;
			},
			googleCode: true,
			param:{
				name:'googleCode'
			}
		}
	},
	submit:{
		url:'/user/login/mergeUser.do?random='+Math.round(Math.random()*100),
		buttonId:"mergeContentSubmitBtn",
		success:function(){
			$("#googleTwiceLoginPop").hide();
			//合并成功，提示用center密码重新登录
			showConfirmDialog("您已经成功使用OKCoin账号合并钱包账号，请使用OKCoin密码重新登录","合并成功",function() {
				window.location.href = '/wallet/index.do';
			},function() {
				window.location.href = '/wallet/index.do';
			});
		},
		failed : function(result){
			error.handleError(result);
		}
	}
});


/**
 * 首页 用户登陆
 */
var optionsGoogle={
	rules:{
		googleTwiceLogincode:{
			required : true,
			googleCode : true,
			param : {
				name:"totpCode"
			}
		},
	},
	submit:{
		url:'/user/login/submitTotpCode.do',
		buttonId:'googleTwiceLoginPopBtn',
		success:function(){
			location.href="/wallet/index.do";
		}
	}
};


var optionsPwd={
	rules:{
		okcoinTwiceLoginPwd:{
			required : true,
			param : {
				name:"loginPwd"
			}
		},
		okcoinTwiceLoginName:{
			param : {
				name:"loginName"
			}
		},
	},
	submit:{
		url:'/user/login/submitTotpCode.do',
		buttonId:'googleTwiceLoginPopBtn',
		success:function(){
			location.href="/wallet/index.do";
		},
		failed : function(result){
			var keyId = Math.abs(result.error.code);
			var key = "error" + keyId;
			var content = languageJson[key]+languageJson['comma']+languageJson['coinOkYourHave']+result.resultCode+languageJson['coinOkChance'];
			$("#okcoinTwiceLoginPwdError").text(content);
			$("#okcoinTwiceLoginPwdError").show();
		}
	}
};


var optionsAll={
	rules:{
		googleTwiceLogincode:{
			required : true,
			googleCode : true,
			param : {
				name:"totpCode"
			}
		},
		okcoinTwiceLoginPwd:{
			required : true,
			param : {
				name:"loginPwd"
			}
		},
		okcoinTwiceLoginName:{
			param : {
				name:"loginName"
			}
		},
	},
	submit:{
		url:'/user/login/submitTotpCode.do',
		buttonId:'googleTwiceLoginPopBtn',
		success:function(){
			location.href="/wallet/index.do";
		},
		failed : function(result){
			var keyId = Math.abs(result.error.code);
			if(keyId == 137){
				var key = "error" + keyId;
				var content = languageJson[key]+languageJson['comma']+languageJson['coinOkYourHave']+result.resultCode+languageJson['coinOkChance'];
				$("#okcoinTwiceLoginPwdError").text(content);
				$("#okcoinTwiceLoginPwdError").show();
			}else{
				error.handleError(result);
			}
		}
	}
};


$("#mergeConfirm").on("click",function(){
	console.log("mergeConfirm click ");
	var url = '/user/login/mergeConfirm.do?random='+Math.round(Math.random()*100);
	$.post(url,null,function(result){
		if(result != null){
			console.log(result);
			$("#mergeContentSubmit").show();
			$("#mergeContent").hide();

			$("#uniform_phone").text(result.data.uniform_phone);
			$("#phone").text(result.data.phone);

			$("#uniform_email").text(result.data.uniform_email);
			$("#email").text(result.data.email);

			if(result.data.messageCodeOpen){
				checkSmsCode = true;
				$("#mergeContentMsgCodeLi").show();
			}

			if(result.data.googleCodeOpen){
				checkGoogleCode = true;
				$("#mergeContentGoogleCodeLi").show();
			}
			$(".googleTwiceLoginPop").css("margin-top","-"+$(".googleTwiceLoginPop").height()/2+"px").css('opacity', 0).show().animate({'opacity' : 1.0}, 300, 'linear');
		}
	},'json');
});


$(function(){
	//显示google二次登录密码弹出层
	$("#loginName,#password").focus(function(){
		$("#loginError").hide();
	});
});

function loginSubmit(){
	$("#loginBtn").click();
}

function _loginSubmit(){
	$("#loginBtn").attr("disabled", true);
	var name = document.getElementById("loginName").value;
	var pwd = document.getElementById("password").value;
	var token = document.getElementById("token").value;
	//TODO:前端需要增加验证
	var desc='';
	if(name==''||pwd==''){
		desc=languageJson['coinOkLoginParamsWrongError'];
		$("#loginError").text(desc);
		$("#loginError").show();
		$("#loginBtn").removeAttr("disabled");
		return;
	}
	setButtonInProcess(true);
	var url = '/user/login/index.do?random='+Math.round(Math.random()*100);
	var param = {
		loginName : name,
		password : pwd,
		token:token
	};

	$.post(url,param,function(result){
		if(result!=null){
			if(result.resultCode==0){  //登陆成功
				forward = $("#forwardUrl").val();
				if( forward == null || ""==forward){
					window.location.href = "/index.do";
				}else{
					forward = decodeURI(forward);
					window.location.href=forward;
				}
			}else if(result.resultCode==1){   //登陆进行GOOGLE验证
				setButtonInProcess(false);
				$("#twiceLoginName").html(name);
				$("#googleTwiceLogincode").val("");
				$("#googleTwiceLogincodeLi").show();
				$(".dialogBoxShadow").show();

				if(result.objectId==1){
					$("#googleTwiceLoginPopContent").hide();
					$("#mergeContent").show();
					$("#mergeContinue").on("click",function(){
						$("#mergeContent").hide();
						$("#googleTwiceLoginPopContent").show();
					});
				}else{
					$("#googleTwiceLoginPopContent").show();
				}

				showDialog("googleTwiceLoginPop");
				validator().validate(optionsGoogle);
			}else if(result.resultCode == -137){   //谷歌、OKCoin账号登录，二次验证
				setButtonInProcess(false);
				$("#twiceLoginName").html(name);
				$("#googleTwiceLogincode").val("");
				$("#okcoinTwiceLoginName").val(name);
				$("#googleTwiceLogincodeLi").show();
				$("#okcoinTwiceLoginPwdLi").show();
				$(".dialogBoxShadow").show();
				$("#googleTwiceLoginPopContent").hide();

				showDialog("googleTwiceLoginPop");

				$("#mergeContent").show();

				$("#mergeContinue").on("click",function(){
					$("#mergeContent").hide();
					$("#googleTwiceLoginPopContent").show();
					validator().validate(optionsAll);
				});
			}else if(result.resultCode == -138){   //OKCoin账号登录，二次验证
				setButtonInProcess(false);
				$("#twiceLoginName").html(name);
				$("#okcoinTwiceLoginName").val(name);
				$("#okcoinTwiceLoginPwdLi").show();
				$(".dialogBoxShadow").show();
				$("#googleTwiceLoginPopContent").hide();

				showDialog("googleTwiceLoginPop");

				$("#mergeContent").show();

				$("#mergeContinue").on("click",function(){
					$("#mergeContent").hide();
					$("#googleTwiceLoginPopContent").show();
					validator().validate(optionsPwd);
				});
			}else if(result.resultCode == -139){   //合并提示
				setButtonInProcess(false);
				$("#twiceLoginName").html(name);
				$("#okcoinTwiceLoginName").val(name);
				$("#okcoinTwiceLoginPwdLi").show();
				$(".dialogBoxShadow").show();
				$("#googleTwiceLoginPopContent").hide();

				showDialog("googleTwiceLoginPop");

				$("#mergeContent").show();

				$("#mergeContinue").on("click",function(){
					console.log("mergeContinue click ");
					var url = '/user/login/submitTotpCode.do?random='+Math.round(Math.random()*100);
					$.post(url,null,function(result){
						if(result != null){
							console.log(result);
							if(result.resultCode == 0){
								window.location.href = '/wallet/index.do';
							}
						}
					},'json');
				});
			}else{   //登陆失败
				setButtonInProcess(false);
				$("#password").val('').focus();
				var content = '';
				var keyId = Math.abs(result.resultCode);
				var key = "error" + keyId;
				if(keyId==19||keyId==22||keyId==109||keyId==27){
					if(result.errorNum > 0){
						content = languageJson[key]+languageJson['comma']+languageJson['coinOkYourHave']+result.errorNum+languageJson['coinOkChance'];
					}else if(Math.abs(result.resultCode)==19){
						content = languageJson["blindphonejs8"];
					}else if(Math.abs(result.resultCode)==22){
						content = languageJson["blindphonejs11"];
					}else{
						content = languageJson["coincommonjs5"];
					}
				}else{
					content = languageJson[key];
				}
				$("#loginError").text(content);
				$("#loginError").show();
				$("#loginBtn").removeAttr("disabled");
				return;
			}
			$("#loginBtn").removeAttr("disabled");
		}
	},'json');
}
/**
 * true处理中;false返回原来
 */
function setButtonInProcess(isInProcess){
	if(isInProcess){
		$("#submitBtn").val('');
		$("#submitBtn").removeClass('btn');
		$("#submitBtn").addClass('waitBtn');
		$("#submitBtn").attr('disabled',true);
	}else{
		$("#submitBtn").val(languageJson['coincommonjs75']);
		$("#submitBtn").removeClass('waitBtn');
		$("#submitBtn").addClass('btn');
		$("#submitBtn").attr('disabled',false);
	}
}